package hl.demosample.ui.fragment.login

import android.os.Bundle
import android.widget.CompoundButton
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import hl.demosample.R
import hl.demosample.app.appViewModel
import hl.demosample.app.base.BaseFragment
import hl.demosample.app.ext.checkMobileAll
import hl.demosample.app.ext.initClose
import hl.demosample.app.ext.showMessage
import hl.demosample.app.util.CacheUtil
import hl.demosample.app.util.LoginAndRegisterAnimation.inputAnimator
import hl.demosample.app.util.LoginAndRegisterAnimation.recovery
import hl.demosample.app.util.SettingUtil
import hl.demosample.databinding.FragmentRegisterBinding
import hl.demosample.viewmodel.request.LoginRegisterViewModel
import hl.demosample.viewmodel.request.RequestLoginRegisterViewModel
import kotlinx.android.synthetic.main.fragment_login.input_layout
import kotlinx.android.synthetic.main.fragment_register.*
import kotlinx.android.synthetic.main.fragment_register.layout_progress
import kotlinx.android.synthetic.main.include_toolbar.*
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.parseState

/*
* path: DemoSample 
* Author: 86133
* Create: 2021-08-17 18:01
* Changes (from 2021-08-17)
* Desc:
*/
class RegisterFragment : BaseFragment<LoginRegisterViewModel, FragmentRegisterBinding>() {

    private val requestLoginRegisterViewModel: RequestLoginRegisterViewModel by viewModels()

    override fun layoutId() = R.layout.fragment_register

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewmodel = mViewModel
        mDatabind.click = ProxyClick()

        toolbar.initClose("注册") {
            nav().navigateUp()
        }

        //设置颜色跟主题颜色一致
        appViewModel.appColor.value?.let {
            SettingUtil.setShapColor(registerSub, it)
            toolbar.setBackgroundColor(it)
        }
    }

    override fun createObserver() {
        super.createObserver()
        requestLoginRegisterViewModel.loginResult.observe(
            viewLifecycleOwner,
            Observer { resultState ->
                parseState(resultState, {
                    //登录成功 通知账户数据发生改变鸟
                    CacheUtil.setUser(it)
                    CacheUtil.setIsLogin(true)
                    nav().navigateUp()
                }, {
                    //注册失败
                    recovery(layout_progress, input_layout)
                    showMessage(it.errorMsg)
                })
            })
    }

    inner class ProxyClick {

        fun clear() {
            mViewModel.username.set("")
        }

        fun register() {
            when {
                mViewModel.username.get().isEmpty() -> showMessage("请填写账号")
                !checkMobileAll(mViewModel.username.get()) -> showMessage("请填写正确的手机号码")
                mViewModel.password.get().isEmpty() -> showMessage("请填写密码")
                mViewModel.password2.get().isEmpty() -> showMessage("请填写确认密码")
                mViewModel.password.get().length < 6 -> showMessage("密码最少6位")
                mViewModel.password.get() != mViewModel.password2.get() -> showMessage("密码不一致")
                else -> {
                    val mWidth = registerSub.measuredWidth
                    val mHeight = registerSub.measuredHeight
                    inputAnimator(input_layout, mWidth.toFloat(), mHeight.toFloat(), input_layout, layout_progress)
                    requestLoginRegisterViewModel.registerAndLogin(mViewModel.username.get(), mViewModel.password.get())
                }
            }
        }

        fun goCancel() {
            recovery(layout_progress, input_layout)
        }

        var onCheckedChangeListener1 =
            CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
                mViewModel.isShowPwd.set(isChecked)
            }

        var onCheckedChangeListener2 = CompoundButton.OnCheckedChangeListener { _, isChecked ->
            mViewModel.isShowPwd2.set(isChecked)
        }
    }
}